#include "UIRenderer.h"

UIRenderer::UIRenderer()
    : m_screenWidth(1280)
    , m_screenHeight(720)
    , m_pLine(NULL)
    , m_pSprite(NULL)
{
}

UIRenderer::~UIRenderer()
{
    Cleanup();
}

bool UIRenderer::Initialize(LPDIRECT3DDEVICE9 device, int screenWidth, int screenHeight)
{
    m_screenWidth = screenWidth;
    m_screenHeight = screenHeight;

    // Create line renderer
    HRESULT hr = D3DXCreateLine(device, &m_pLine);
    if (FAILED(hr))
        return false;

    // Create sprite renderer
    hr = D3DXCreateSprite(device, &m_pSprite);
    if (FAILED(hr))
        return false;

    return true;
}

void UIRenderer::Cleanup()
{
    if (m_pSprite)
    {
        m_pSprite->Release();
        m_pSprite = NULL;
    }
    if (m_pLine)
    {
        m_pLine->Release();
        m_pLine = NULL;
    }
}

void UIRenderer::RenderCrosshair(LPDIRECT3DDEVICE9 device)
{
    if (!m_pLine)
        return;

    // Screen center (准星稍微向上偏移 30 像素)
    float centerX = m_screenWidth / 2.0f;
    float centerY = m_screenHeight / 2.0f - 30.0f;  // 向上移动 30px

    // Crosshair parameters (增大尺寸，提高可见性)
    float size = 30.0f;      // 从20增大到30
    float gap = 8.0f;        // 从5增大到8
    float thickness = 3.0f;  // 从2增大到3

    D3DXVECTOR2 vertices[2];

    // 先画黑色外边框（描边效果）
    D3DCOLOR outlineColor = D3DCOLOR_ARGB(255, 0, 0, 0);  // 黑色边框
    m_pLine->SetWidth(thickness + 2.0f);  // 比主线粗2像素
    m_pLine->Begin();

    // Horizontal line - left (outline)
    vertices[0] = D3DXVECTOR2(centerX - size, centerY);
    vertices[1] = D3DXVECTOR2(centerX - gap, centerY);
    m_pLine->Draw(vertices, 2, outlineColor);

    // Horizontal line - right (outline)
    vertices[0] = D3DXVECTOR2(centerX + gap, centerY);
    vertices[1] = D3DXVECTOR2(centerX + size, centerY);
    m_pLine->Draw(vertices, 2, outlineColor);

    // Vertical line - top (outline)
    vertices[0] = D3DXVECTOR2(centerX, centerY - size);
    vertices[1] = D3DXVECTOR2(centerX, centerY - gap);
    m_pLine->Draw(vertices, 2, outlineColor);

    // Vertical line - bottom (outline)
    vertices[0] = D3DXVECTOR2(centerX, centerY + gap);
    vertices[1] = D3DXVECTOR2(centerX, centerY + size);
    m_pLine->Draw(vertices, 2, outlineColor);

    m_pLine->End();

    // 再画白色主线（高对比度）
    D3DCOLOR mainColor = D3DCOLOR_ARGB(255, 255, 255, 255);  // 纯白色
    m_pLine->SetWidth(thickness);
    m_pLine->Begin();

    // Horizontal line - left
    vertices[0] = D3DXVECTOR2(centerX - size, centerY);
    vertices[1] = D3DXVECTOR2(centerX - gap, centerY);
    m_pLine->Draw(vertices, 2, mainColor);

    // Horizontal line - right
    vertices[0] = D3DXVECTOR2(centerX + gap, centerY);
    vertices[1] = D3DXVECTOR2(centerX + size, centerY);
    m_pLine->Draw(vertices, 2, mainColor);

    // Vertical line - top
    vertices[0] = D3DXVECTOR2(centerX, centerY - size);
    vertices[1] = D3DXVECTOR2(centerX, centerY - gap);
    m_pLine->Draw(vertices, 2, mainColor);

    // Vertical line - bottom
    vertices[0] = D3DXVECTOR2(centerX, centerY + gap);
    vertices[1] = D3DXVECTOR2(centerX, centerY + size);
    m_pLine->Draw(vertices, 2, mainColor);

    m_pLine->End();
}

void UIRenderer::RenderHealthBar(LPDIRECT3DDEVICE9 device, float currentHealth, float maxHealth, float x, float y, float width, float height)
{
    if (!m_pLine)
        return;

    // Calculate health percentage
    float healthPercent = currentHealth / maxHealth;
    if (healthPercent < 0.0f) healthPercent = 0.0f;
    if (healthPercent > 1.0f) healthPercent = 1.0f;

    // Colors
    D3DCOLOR backgroundColor = D3DCOLOR_ARGB(180, 50, 50, 50);
    D3DCOLOR healthColor;

    if (healthPercent > 0.6f)
        healthColor = D3DCOLOR_ARGB(255, 0, 255, 0);  // Green
    else if (healthPercent > 0.3f)
        healthColor = D3DCOLOR_ARGB(255, 255, 255, 0);  // Yellow
    else
        healthColor = D3DCOLOR_ARGB(255, 255, 0, 0);  // Red

    // Draw background
    DrawRect(device, x, y, width, height, backgroundColor, true);

    // Draw health
    float healthWidth = width * healthPercent;
    DrawRect(device, x, y, healthWidth, height, healthColor, true);

    // Draw border
    DrawRect(device, x, y, width, height, D3DCOLOR_ARGB(255, 255, 255, 255), false);
}

void UIRenderer::RenderDamageIndicator(LPDIRECT3DDEVICE9 device, float intensity)
{
    if (!m_pLine || intensity <= 0.0f)
        return;

    if (intensity > 1.0f) intensity = 1.0f;

    // Red color with varying alpha
    BYTE alpha = (BYTE)(intensity * 150);
    D3DCOLOR color = D3DCOLOR_ARGB(alpha, 255, 0, 0);

    float thickness = 10.0f;
    m_pLine->SetWidth(thickness);
    m_pLine->Begin();

    D3DXVECTOR2 vertices[2];

    // Top edge
    vertices[0] = D3DXVECTOR2(0, 0);
    vertices[1] = D3DXVECTOR2((float)m_screenWidth, 0);
    m_pLine->Draw(vertices, 2, color);

    // Bottom edge
    vertices[0] = D3DXVECTOR2(0, (float)m_screenHeight);
    vertices[1] = D3DXVECTOR2((float)m_screenWidth, (float)m_screenHeight);
    m_pLine->Draw(vertices, 2, color);

    // Left edge
    vertices[0] = D3DXVECTOR2(0, 0);
    vertices[1] = D3DXVECTOR2(0, (float)m_screenHeight);
    m_pLine->Draw(vertices, 2, color);

    // Right edge
    vertices[0] = D3DXVECTOR2((float)m_screenWidth, 0);
    vertices[1] = D3DXVECTOR2((float)m_screenWidth, (float)m_screenHeight);
    m_pLine->Draw(vertices, 2, color);

    m_pLine->End();
}

void UIRenderer::DrawLine(LPDIRECT3DDEVICE9 device, float x1, float y1, float x2, float y2, D3DCOLOR color)
{
    if (!m_pLine)
        return;

    m_pLine->Begin();
    D3DXVECTOR2 vertices[2];
    vertices[0] = D3DXVECTOR2(x1, y1);
    vertices[1] = D3DXVECTOR2(x2, y2);
    m_pLine->Draw(vertices, 2, color);
    m_pLine->End();
}

void UIRenderer::DrawRect(LPDIRECT3DDEVICE9 device, float x, float y, float width, float height, D3DCOLOR color, bool filled)
{
    if (!m_pLine)
        return;

    if (filled)
    {
        // Draw filled rectangle as multiple horizontal lines
        m_pLine->SetWidth(1.0f);
        m_pLine->Begin();

        for (int i = 0; i < (int)height; i++)
        {
            D3DXVECTOR2 vertices[2];
            vertices[0] = D3DXVECTOR2(x, y + i);
            vertices[1] = D3DXVECTOR2(x + width, y + i);
            m_pLine->Draw(vertices, 2, color);
        }

        m_pLine->End();
    }
    else
    {
        // Draw border
        m_pLine->SetWidth(2.0f);
        m_pLine->Begin();

        D3DXVECTOR2 vertices[5];
        vertices[0] = D3DXVECTOR2(x, y);
        vertices[1] = D3DXVECTOR2(x + width, y);
        vertices[2] = D3DXVECTOR2(x + width, y + height);
        vertices[3] = D3DXVECTOR2(x, y + height);
        vertices[4] = D3DXVECTOR2(x, y);
        m_pLine->Draw(vertices, 5, color);

        m_pLine->End();
    }
}
